<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );
global $dal;

$query = "SELECT admin " .
		"FROM users " .
		"WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array($result, MYSQL_ASSOC);
if( isset( $row[ 'admin' ] ) && $row[ 'admin' ] == 1 ) {
	if( isset( $_POST[ 'confirm_box' ] ) ) {
		$statement = $dal->prepare( 'SELECT DISTINCT user_id ' .
				'FROM translations ' .
				'WHERE approved = 1' );
		$users = $dal->getArrayOfObjects( $statement );
	
		$statement = $dal->prepare( 'SELECT count(*) ' .
				'FROM translations ' .
				'WHERE user_id = :user_id AND approved = 1' );
		$userId = -1;
		$statement->bindParam( ':user_id', $userId, PDO::PARAM_INT );
		$statement2 = $dal->prepare( 'SELECT ruler_id ' .
				'FROM ruler ' .
				'WHERE user_id = :user_id' );
		$statement2->bindParam( ':user_id', $userId, PDO::PARAM_INT );
		$statement3 = $dal->prepare( 'SELECT premium ' .
				'FROM users ' .
				'WHERE user_id = :user_id' );
		$statement3->bindParam( ':user_id', $userId, PDO::PARAM_INT );
		$statement4 = $dal->prepare( 'UPDATE users ' .
				'SET premium = :premium ' .
				'WHERE user_id = :user_id' );
		$statement4->bindParam( ':user_id', $userId, PDO::PARAM_INT );
		$premiumTurns = -1;
		$statement4->bindParam( ':premium', $premiumTurns, PDO::PARAM_INT );
		$date = getCurrentDate();
		$turn = getCurrentTurn();
		foreach( $users AS $user ) {
			$userId = $user->user_id;
			$translationCount = $dal->getValue( $statement );

			$rulerId = $dal->getValue( $statement2 );
			if( isset( $rulerId ) && is_numeric( $rulerId ) ) {
				$title = 'Premium turns for translation work';
				$text = 'As reward for your translation work you have been given ' . number_format( $translationCount ) . ' premium turns.' . "\n\n" .
						'Thanks for your work!';
				
				$query = "INSERT INTO news " .
						"( `ruler_id`, `type`, `news`, `date`, `turn`, `subject` ) " .
						"VALUES ( $rulerId, 0, '$text', '$date', $turn, '$title' )";
				mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			}
			
			$premium = $dal->getValue( $statement3 );
			if( $premium == -1 ) {
				$premium = $translationCount;
			}
			else {
				$premium += $translationCount;
			}
			$premiumTurns = $premium;
			$dal->execute( $statement4 );
		}
		
		if( count( $users ) > 0 ) {
			$title = "Translators Rewarded";
			$text = "All " . count( $users ) . " translators have been given 1 premium turn for every approved and translated text they submitted.\n" .
					"\n" .
					"Thanks for your efforts!";
			
			$query = "INSERT INTO history " .
					"( `title`, `text`, `ruler_id`, `visible`, `date`, `turn`, `allow_comments` ) " .
					"VALUES ( '$title', '$text', -1, 1, '$date', $turn, 1 )";
			mysql_query( $query ) or die( "Query failed : " . mysql_error() );

			$query = "SELECT history_id " .
					"FROM history " .
					"WHERE title = '$title' AND text = '$text' " .
					"AND turn = $turn AND ruler_id = -1";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			$history_id = $row[ 'history_id' ];
			
		   $query = "SELECT ruler_id " .
		    		"FROM users " .
		    		"JOIN ruler ON users.user_id = ruler.user_id " .
		    		"WHERE highlight_history = 1";
		   $result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		   $num = mysql_numrows( $result );
			$i = 0;
			while ( $i < $num ) {
				$query3 = "INSERT INTO history_seen " .
						"( ruler_id, history_id ) " .
						"VALUES( " . mysql_result( $result, $i, "ruler_id" ) . ", $history_id )";
				mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
				$i ++;
			}
		}
		$statement = $dal->prepare( 'UPDATE game ' .
				'SET translations_timestamp = :time' );
		$time = time();
		$statement->bindParam( ':time', $time, PDO::PARAM_INT );
		$dal->execute( $statement );
	}
	header( 'Location: languages.php' );
}
else {
	header( 'Location: ../index.php' );
}	
?>